package org.ganesh.android;



import org.ksoap2.serialization.SoapObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import classes.DownloadStatus;

public class AndrailActivity extends Activity {
    /** Called when the activity is first created. */
	
	 ProgressDialog pd=null;
	
	/*
	 * Event Handler on Submit Button click event
	 */
	public void btnSubmit_Click(View v){
		
		 pd = ProgressDialog.show(AndrailActivity.this, 
                "Please wait...", "We're trying to find the current status", true);
		new Thread(new Runnable(){
            @Override
			public void run(){
		Intent i = new Intent(AndrailActivity.this,TrainStatus.class);
		Bundle bundle = new Bundle();
		
		EditText etTrainNumber = (EditText)findViewById(R.id.etTrainNumber);
		EditText etStationCode = (EditText)findViewById(R.id.etStationCode);
		String trainNumber=etTrainNumber.getText().toString();
		String stationCode=etStationCode.getText().toString();
		DatePicker dpTravelDate = (DatePicker)findViewById(R.id.dpTravelDate);
		String dd = null;
		String mm=null;
		if (dpTravelDate.getDayOfMonth()<10) 
		{
			dd="0"+ Integer.toString(dpTravelDate.getDayOfMonth());
		}
		else
		{
			dd=Integer.toString(dpTravelDate.getDayOfMonth());
		}
		
		if (dpTravelDate.getMonth()+1 <10)
		{
			mm="0"+ Integer.toString(dpTravelDate.getMonth()+1);
		}
		else
		{
			mm=Integer.toString(dpTravelDate.getMonth()+1);
		}
		String travelDate = dd + "/" + mm+ "/" + Integer.toString(dpTravelDate.getYear());
		
		DownloadStatus ds = new DownloadStatus();
		try{
			
		
		SoapObject result=ds.GetData(trainNumber, stationCode, travelDate);
		
		bundle.putString("resultTrainName", result.getProperty(0).toString());
		bundle.putString("resultstationName", result.getProperty(1).toString());
		bundle.putString("resultSTA", result.getProperty(2).toString());
		bundle.putString("resultDelay", result.getProperty(3).toString());
		bundle.putString("resultExptArv", result.getProperty(4).toString());
		bundle.putString("resultSDT", result.getProperty(5).toString());
		bundle.putString("resultDelayDepart", result.getProperty(6).toString());
		bundle.putString("resultExpectedDepart", result.getProperty(7).toString());
		bundle.putString("resultLastUpdated", result.getProperty(8).toString());
		i.putExtras(bundle);
		
		}
		catch (Exception e){
			
		}
		/*
	
		bundle.putString("trainNumber", etTrainNumber.getText().toString());
		bundle.putString("stationCode", etStationCode.getText().toString());
		bundle.putString("travelDate", travelDate);
	    i.putExtras(bundle);*/
		startActivity(i);
		
		pd.dismiss();
            }
		
		}).start();
		
	}
	/*
	 * OnCreate Method starts here
	 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
    }
}